package enemies;

import java.awt.Component;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Rectangle;
import java.awt.RenderingHints;
import java.awt.geom.AffineTransform;
import java.awt.geom.Point2D;
import java.util.ArrayList;

import javax.swing.ImageIcon;

public abstract class MobileEnemy extends Enemy {

	protected ImageIcon deviceImage;
	protected ImageIcon nukedImage2;

	protected int deviceAngle = 0;

	MobileEnemy(ArrayList<Point2D.Double> route, Point2D.Double position,
			int health, Dimension size, ImageIcon image, int firerate,
			int range, int speed, int damageMultiplier, ImageIcon deviceImage, int deviceAngle, ImageIcon nukedImage2) {
		super(route, position, health, size, image, firerate, range, speed, damageMultiplier);
		this.deviceAngle = deviceAngle;
		this.deviceImage = deviceImage;
		this.nukedImage2 = nukedImage2;
	}

	public int getDeviceAngle() {
		return deviceAngle;
	}

	public void setDeviceAngle(int d) {
		deviceAngle = d;
	}

	public boolean update() {
		super.update();
		
		if(!this.nuked){
			// Richt loop op toren
			if (aimAtTower != null) {
				double x = aimAtTower.getPosition().x - position.x;
				double y = aimAtTower.getPosition().y - position.y;
				
				// tower links van enemy
				if (x < 0) {
					// tower hoger dan enemy
					x *= -1;
					if (y < 0) {
						y *= -1;
						deviceAngle = (int) Math.toDegrees((Math.atan((y / x)))) + 90;
					}
					// tower lager dan enemy
					else if (y >= 0) {
						deviceAngle = (int) Math.toDegrees((Math.atan((x / y))));
					}
					// tower rechts van enemy
				} else if (x >= 0) {
					if (y < 0) {
						y *= -1;
						deviceAngle = (int) Math.toDegrees((Math.atan((x / y)))) + 180;
					} else if (y >= 0) {
						deviceAngle = (int) Math.toDegrees((Math.atan((y / x)))) + 270;
					}
				}
			} else {
				deviceAngle = this.viewAngle;
			}
		}
		
		return true;
	}
	
	public void draw(Graphics g, Rectangle area, Component c ) {
		if ( isOutsideView() || nextDestination == 0 )
			return;
		
		super.draw(g, area, c);
		
		if ( this.deviceAngle == 0 )
			g.drawImage(deviceImage.getImage(), (int)this.position.x+(image.getIconWidth()/2)-deviceImage.getIconWidth()/2, (int)this.position.y + (image.getIconHeight()/2)-deviceImage.getIconHeight()/2, c );
		else {
			Graphics2D g2d = (Graphics2D)g;
		    g2d.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BICUBIC);
	
		    AffineTransform oldForm = g2d.getTransform();
		    AffineTransform newForm = (AffineTransform) g2d.getTransform().clone();
		    
		    newForm.rotate(Math.toRadians(this.deviceAngle), (int)getMiddle().x, (int)getMiddle().y);
		    
		    g2d.setTransform(newForm);
		    if(nuked)
		    	g2d.drawImage(nukedImage2.getImage(), (int)this.position.x+(image.getIconWidth()/2)-deviceImage.getIconWidth()/2, (int)this.position.y + (image.getIconHeight()/2)-deviceImage.getIconHeight()/2, c );    	
		    else	
		    	g2d.drawImage(deviceImage.getImage(), (int)this.position.x+(image.getIconWidth()/2)-deviceImage.getIconWidth()/2, (int)this.position.y + (image.getIconHeight()/2)-deviceImage.getIconHeight()/2, c );
			g2d.setTransform(oldForm);
		}
	}
}
